package models;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;

import play.data.validation.Constraints.Pattern;
import play.data.validation.Constraints.Required;
import play.db.ebean.Model;

@Entity
@Table(name = "projects")
public class Project extends Model {

	private static final long serialVersionUID = 1L;

	@Column(name = "id")
	@GeneratedValue(generator = "SEQ_TABLE", strategy = GenerationType.TABLE)
	@Id
	@TableGenerator(name = "SEQ_TABLE")
	private long id;

	@Column(name = "company", nullable = false)
	@ManyToOne(fetch = FetchType.LAZY)
	private Company company;

	@Column(name = "name", nullable = false)
	@Pattern(value = "[A-Za-z0-9\\-_][A-Za-z0-9\\-_ ]+", message = "The name contains invalid characters. Valid characters are: A-Z, 0-9, hyphen (-), underscore (_) and white space ( ).")
	@Required
	private String name;

	@Column(name = "description")
	private String description;

	@Column(name = "default_config")
	@JoinTable(name = "resources")
	@ManyToOne(fetch = FetchType.LAZY)
	private Resource defaultConfiguration;

	@Column(name = "created")
	private Date created;

	@Column(name = "last_modified")
	private Date lastModified;

	@Column(name = "tags")
	@OneToMany( fetch = FetchType.LAZY)
	private List<ScheduleJob> scheduleJobs;
	
	@Column(name = "tags")
	@OneToMany( fetch = FetchType.LAZY)
	private List<Tag> tags;
	
	public static final Finder<Long, Project> find = new Finder<Long, Project>(Long.class, Project.class);

	public Project(Company company, String name) {
		this.company = company;
		this.name = name;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Resource getDefaultConfiguration() {
		return defaultConfiguration;
	}

	public void setDefaultConfiguration(Resource defaultConfiguration) {
		this.defaultConfiguration = defaultConfiguration;
	}

	public Date getCreatedDate() {
		return created;
	}

	public void setCreatedDate(Date created) {
		this.created = created;
	}

	public Date getLastModifiedDate() {
		return lastModified;
	}

	public void setLastModifiedDate(Date lastModified) {
		this.lastModified = lastModified;
	}

	public static Project findById(long projectId) {
		return find.byId(projectId);
	}

	public static List<Project> findProjectsByCompany(Company company) {
		return find.where().eq("company.id", company.getId()).findList();
	}

	public Date getCreated()
	{
		return created;
	}

	public void setCreated(Date created)
	{
		this.created = created;
	}

	public Date getLastModified()
	{
		return lastModified;
	}

	public void setLastModified(Date lastModified)
	{
		this.lastModified = lastModified;
	}

	public List<ScheduleJob> getScheduleJobs()
	{
		return scheduleJobs;
	}

	public void setScheduleJobs(List<ScheduleJob> scheduleJobs)
	{
		this.scheduleJobs = scheduleJobs;
	}

	public List<Tag> getTags()
	{
		return tags;
	}

	public void setTags(List<Tag> tags)
	{
		this.tags = tags;
	}

	@Override
	public String toString()
	{
		return "Project [id=" + id + ", company=" + company + ", name=" + name
				+ ", description=" + description + ", defaultConfiguration="
				+ defaultConfiguration + ", created=" + created
				+ ", lastModified=" + lastModified + ", scheduleJobs="
				+ scheduleJobs + ", tags=" + tags + "]";
	}
	


	

}
